package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.UserDaoImpl;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminUserServlet extends BaseServlet {
	/**
	 * localhost:8080/shangcheng/adminUser?reqName=getMessageByStatus
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getMessageByStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		String status = request.getParameter("status");
		List<User> list = uDao.getUsersByStatus(status);
		
		Gson gson = new Gson();
		String res = gson.toJson(list);
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminUser?reqName=getMessageById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		User user = uDao.getUsersById(id);
		
		Gson gson = new Gson();
		String res = gson.toJson(user);
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminUser?reqName=updateMessageById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		
		User user = uDao.getUsersById(id);
		if (user.getStatus().equals("已读") || user.getStatus().equals("已回复")) {
			out.print(Result.toClient(true, "", ""));
		} else { //只有未读情况才要修改
			String status = request.getParameter("status");
			user = new User(id, status, null);
			int n = uDao.updateMessage(user);
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		}
		
		
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminUser?reqName=updateMessageReplyById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMessageReplyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		String reply = request.getParameter("reply");
		User user = new User(id, null, reply);
		int n = uDao.updateMessageReply(user);
		
		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		
		out.flush();
		out.close();
	}

	
	/**
	 * localhost:8080/shangcheng/adminUser?reqName=deleteMessageByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteMessageByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		String ids = request.getParameter("ids");//3121111,12
		int n = uDao.delMessageByIds(ids);
		
		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
	}
}
